package com.openthinks.joyniproject;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.openthinks.joyniproject.model.Dept;
import com.openthinks.joyniproject.model.Role;
import com.openthinks.joyniproject.service.DeptService;
import com.openthinks.joyniproject.service.RoleService;
import com.openthinks.joyniproject.utils.concurrent.ConcurrentMap;

/**
 * 静态数据加载
 * @since 
 *		v1.0
 * @version
 * 		v1.0, 2013-06-28 21:42:30
 * @author 
 *		Cloud
 */
@Component
public class DataLoader {

	@Autowired
	private DeptService deptService;
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 部门数据
	 */
	private static ConcurrentMap<Long, Dept> deptData = new ConcurrentMap<Long, Dept>();
	
	/**
	 * 角色数据
	 */
	private static ConcurrentMap<Long, Role> roleData = new ConcurrentMap<Long, Role>();
	
	
	/**
	 * 初始化
	 */
	public void init() {
		
		initDept();
		initRole();
	}
	
	/**
	 * 初始化部门数据
	 */
	private void initDept() {
		
		ConcurrentMap<Long, Dept> deptData = new ConcurrentMap<Long, Dept>();
		List<Dept> list = deptService.findAll();
		for (Dept bean : list) {
			deptData.put(bean.getId(), bean);
		}
		DataLoader.setDeptData(deptData);
	}
	
	/**
	 * 初始化角色数据
	 */
	private void initRole() {
		
		ConcurrentMap<Long, Role> roleData = new ConcurrentMap<Long, Role>();
		List<Role> list = roleService.findAll();
		for (Role bean : list) {
			roleData.put(bean.getId(), bean);
		}
		DataLoader.setRoleData(roleData);
	}

	/**
	 * 获取部门数据
	 * @return 
	 * 		部门数据
	 */
	public static ConcurrentMap<Long, Dept> getDeptData() {
		return deptData;
	}

	/**
	 * 设置部门数据
	 * @param deptData
	 * 			部门数据
	 */
	public static void setDeptData(ConcurrentMap<Long, Dept> deptData) {
		DataLoader.deptData = deptData;
	}

	/**
	 * 获取角色数据
	 * @return 
	 * 		角色数据
	 */
	public static ConcurrentMap<Long, Role> getRoleData() {
		return roleData;
	}

	/**
	 * 设置角色数据
	 * @param roleData
	 * 			角色数据
	 */
	public static void setRoleData(ConcurrentMap<Long, Role> roleData) {
		DataLoader.roleData = roleData;
	}
	
}
